#include "Player.h"

#include "Utils.h"
#include "Enemy.h"
#include <Windows.h>
#include <gl/GL.h>
#include <SDL.h>

#include "Audio.h"
#include "Castle.h"

void Player::update( float frametime )
{
	if( m_movementMap.reset ){
		m_pos = Position(1600, -145);
		m_haswon = false;
		m_dead = false;

		Audio::getInstance().play("music/mario_4.ogg");
	}

	// Doe niets meer als je gewonnen hebt
	if( m_haswon )
		return;

//wanneer mario naar beneden valt	
	if(m_pos.y < -300 && !m_dead){
		m_dead = true;
		Audio::getInstance().play("music/mario_6.ogg");
	}


	m_movementMap.reset = false;

	if(!m_dead){

		// Movement
		if(m_movementMap.left ) m_speed.x -= 8;
		if(m_movementMap.right ) m_speed.x += 8;

		//jump
		if(m_speed.y >= 0){ //wanneer player niet aan het vallen is
			if(m_movementMap.jump && (SDL_GetTicks() < m_endJumpTime || m_endJumpTime == 0)){ //jump is ingedrukt binnen jumptime
				m_speed.y = 0;
				m_speed.y = (m_endJumpTime - SDL_GetTicks()) *0.25;
				m_jumping = true;
				if(m_endJumpTime == 0)
					m_endJumpTime = SDL_GetTicks() + 0.7 * 1000;
			}
			if(!m_movementMap.jump && m_jumping){ //jump wordt losgelaten
				m_endJumpTime = SDL_GetTicks()+1; //jump is losgelaten
			}
			if( m_endJumpTime != 0 && SDL_GetTicks() >= m_endJumpTime){
				m_speed.y -= 5;
			}
		}else{
			//m_speed.y -= 5;
			m_endJumpTime = 0;
		}
	}
	// Friction
	m_speed.x *= 0.95;

	// Max speed
	m_speed.x = (m_speed.x > 0 ? 1 : -1 ) * MIN( abs(m_speed.x), 50 );
	m_speed.y = (m_speed.y > 0 ? 1 : -1 ) * MIN( abs(m_speed.y), 200 );

	// Gravity
	if(!m_intersecting)
		m_speed.y -= 10;

	m_pos.x += m_speed.x * frametime;
	m_pos.y += m_speed.y * frametime;

	// Are we going left or right?
	if( m_speed.x > 1  )
		m_right = true;
	else if( m_speed.x < -1  )
		m_right = false;

	m_intersecting = false;

	m_texture.setMirror(m_right);
}

void Player::setMovementMap( const MovementMap& map )
{
	m_movementMap = map;
}

Player::Player() : Quad(Position())
{
	m_texture.setFilename("textures/sario3.png");
	m_endJumpTime = 0;
	m_jumping = false;
	m_width = 10;
	m_height = 10;
	m_right = true;
	m_intersecting = true;
	m_dead = false;
	m_haswon = false;
}

Player::Player(Position p) : Quad(p)
{
	m_texture.setFilename("textures/sario3.png");
	m_endJumpTime = 0;
	m_jumping = false;
	m_width = 10;
	m_height = 10;
	m_right = true;
	m_intersecting = true;
	m_dead = false;
	m_haswon = false;
}

void Player::setIntersect( Entity * h, int side )
{
	if(m_dead)
		return;

	Castle* c = dynamic_cast<Castle*>(h);
	if( c && !m_haswon )
	{
		m_haswon = true;
		Audio::getInstance().play("music/mario_7.ogg");
		return;
	}

	if(side == 3|| side == 4){
		//sario bots met zijn kop

		Enemy * e;
		if((e = dynamic_cast<Enemy*>(h)) && !e->isDead() ){
			//op object gesprongen.
			setDead();

			return; // Doe verder niets
		}else{

			m_speed.y = -5;

			m_intersecting = false;
		}
	}

	if(side == 1|| side == 2){
		//sario heeft op dit object gebotst.
		
		Enemy * e;
		if(e = dynamic_cast<Enemy*>(h) ){
			//op object gesprongen.
			e->setDead();
			m_speed.y += 200;
		}else{
			m_speed.y = 0;
			m_jumping = false;
			m_intersecting = true;

		}
	}
}





bool Player::isDead(){
	return m_dead;
}

void Player::setDead()
{
	m_dead = true;
	m_speed.y = 250;
	m_speed.x = 1;

	int r = rand() % 2;
	switch( r )
	{
	case 0:
		Audio::getInstance().play("music/mario_5.ogg");
		break;

	case 1:
		Audio::getInstance().play("music/mario_6.ogg");
		break;
	}
}